<?php

namespace app\models;

use app\traits\ModelTrait;
use Phalcon\Mvc\Model;

/**
 * Class Blog
 * @package app\models
 * @property integer id
 * @property string title
 * @property string content
 * @property integer author
 * @property integer created_at
 * @property integer updated_at
 */
class Blog extends Model
{
    use ModelTrait;

    public $title;

    public $content;

    const TABLE = 'blog';

    public function beforeValidation()
    {
        if (empty($this->title)) {
            self::$error = '标题不能为空';
            return false;
        }
        if (strlen($this->title) > 255) {
            self::$error = '标题长度限制255个字节';
            return false;
        }
        if (empty($this->content)) {
            self::$error = '内容不能为空';
            return false;
        }
        return true;
    }

    public function beforeCreate()
    {
        $this->created_at = time();
        $this->updated_at = time();
        $user = $this->getDI()->getShared('session')->get('user');
        $this->author = $user['id'];
    }

    public static function getShowContent($content)
    {
        $content = htmlspecialchars($content);
        return mb_substr($content, 0 ,50);
    }
}